Ext.define('CA.dictionary.Panel',{
    extend: 'Ext.form.Panel',
    xtype: 'CA.dictionary.Panel',

    uses: [
        'CA.dictionary.MetaTable'
    ],

    initComponent: function(){
        var metaStore = Ext.create('Ext.data.Store',{
            fields: ['code','type','length','def','old']
        });

        Ext.apply(this, {
            style: 'padding-bottom: 5px;background:#DFE8F6;',
            padding: 5,
            layout: 'border',
            fieldDefaults: {
                labelAlign: 'top',
                msgTarget: 'side'
            },
            items: [
                {
                    region: 'north',
                    split: true,
                    bodyPadding: 15,
                    layout: 'anchor',
                    defaults: { anchor:'100%' },
                    items: [
                        {
                            xtype: 'textfield',
                            fieldLabel: 'Наименование',
                            allowBlank: false,
                            name: 'name'
                        },
                        {
                            xtype: 'textfield',
                            fieldLabel: 'Код',
                            allowBlank: false,
                            name: 'code'
                        },
                        {
                            xtype: 'CA.base.LocaleCombo',
                            locale: CA.locale.dictionaryTypes,
                            fieldLabel: 'Тип',
                            name: 'view_type',
                            value: 'line',
                            allowBlank: false
                        },
                        {
                            fieldLabel: 'Поле отображения',
                            xtype: 'combo',
                            store: metaStore,
                            queryMode: 'local',
                            displayField: 'code',
                            valueField: 'code',
                            name: 'field',
                            allowBlank: false,
                            editable: false
                        }
                    ]
                },
                {
                    region: 'center',
                    itemId: 'metaTable',
                    xtype: 'CA.dictionary.MetaTable',
                    store: metaStore
                }
            ],
            buttons: [
                {
                    text: 'Сохранить',
                    scope: this,
                    handler: this.doApply
                },
                {
                    text: 'Сбросить',
                    scope: this,
                    handler: this.doReset
                }
            ]
        });

        if(this.editMode){
            this.on('activate', this.onActivate, this);
        }

        this.callParent();
    },

    onActivate: function(){
        if(this.isFirstLoad == true){
            return;
        }

        this.isFirstLoad = true;
        this.doLoad();
    },

    doLoad: function(){

        var el = this.getEl(),
            form = this.getForm();

        el.mask('Загрузка...');

        Dictionary.load(this.dictionaryId, function(resp){
            el.unmask();
            this.prepareResponse(resp);
        }, this);
    },

    prepareResponse:function(resp){
        var form = this.getForm(),
            metaStore = this.down('#metaTable').getStore();

        form.setValues(resp.data);
        metaStore.loadData(resp.meta);

        this.dictionaryCode = resp.data.code;
    },

    doApply: function(){
        var el = this.getEl(),
            form = this.getForm(),
            metaTable = this.down('#metaTable'),
            meta = metaTable.getMeta(),
            vals = form.getValues(),
            valid = metaTable.isValid() && form.isValid();

        if(!valid){
            return;
        }

        vals.field = vals.field.toUpperCase();

        el.mask('Сохранение...');

        if(this.editMode){
            Dictionary.update(this.dictionaryId, this.dictionaryCode, vals, meta, function(resp){
                el.unmask();

                if(CA.checkResponse(resp)){
                    this.prepareResponse(resp);
                    this.setTitle(resp.data.name);
                    this.fireEvent('saveDictionary', resp.data);
                }
            }, this);
        } else {
            Dictionary.create(vals, meta, function(resp){
                el.unmask();

                if(CA.checkResponse(resp)){
                    this.prepareResponse(resp);
                    this.fireEvent('saveDictionary', resp.data);
                    this.doReset();
                }
            }, this);
        }
    },

    doReset: function(){
        if(this.editMode){
            this.doLoad();
        } else {
            this.getForm().reset();
            this.down('#metaTable').getStore().removeAll();
        }
    }
});
